<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { CirclePlus, Delete } from '@element-plus/icons-vue'
import { type OrgNode, type AddOrg } from '@/api/sys'
import { getOrgTree, addOrg, delOrg } from '@/api/sys'
import dayjs from 'dayjs'

defineOptions({
  name:"Org"
})

const tableData = ref<OrgNode[]>([])

const isAddDialog = ref(true) // 是否为添加模式
const showDialog = ref(false) // 是否显示模态框
const dialogForm = reactive<AddOrg>({})

onMounted(() => {
  clickSearchBtn()
})
const clickSearchBtn = () => {
  getOrgTree().then((res) => {
    tableData.value = res
  })
}
const clickEditBtn = (id: string) => {
  dialogForm.parentId = id
  dialogForm.orgName = ''
  dialogForm.status = 1
  showDialog.value = true
}
const clickSubDialogForm = () => {
  addOrg({
    parentId: dialogForm.parentId,
    orgName: dialogForm.orgName,
    status: dialogForm.status,
  }).then(() => {
    clickSearchBtn()
    showDialog.value = false
  })
}

const clickDelBtn = (id: string) => {
  delOrg(id).then(() => {
    clickSearchBtn()
  })
}
</script>

<template>
  <div>
    <el-dialog
      v-model="showDialog"
      :title="isAddDialog ? '添加组织信息' : '更新组织信息'"
      width="500"
    >
      <el-form :model="dialogForm" label-width="auto" style="max-width: 600px">
        <el-form-item label="账户" v-show="isAddDialog">
          <el-input v-model="dialogForm.orgName" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="dialogForm.status">
            <el-radio :value="1">正常</el-radio>
            <el-radio :value="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="clickSubDialogForm"> 提交 </el-button>
        </div>
      </template>
    </el-dialog>
    <el-row>
      <el-col>
        <el-card>
          <el-table :data="tableData" row-key="id" border default-expand-all>
            <el-table-column prop="orgName" label="名称" sortable />
            <el-table-column prop="mate.creator" label="创建人" sortable />
            <el-table-column label="状态" sortable>
              <template #default="scope">
                <el-tag :type="scope.row.mate.status == 1 ? 'success' : 'info'">{{
                  scope.row.mate.status == 1 ? '可用' : '禁用'
                }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="创建时间" sortable>
              <template #default="scope">
                {{ dayjs(new Date(scope.row.mate.createdAt * 1000)).format('YYYY-MM-DD HH:mm:ss') }}
              </template>
            </el-table-column>
            <el-table-column label="操作" sortable>
              <template #default="scope">
                <el-link
                  :icon="CirclePlus"
                  class="mr-3"
                  type="primary"
                  @click="clickEditBtn(scope.row.id)"
                  >添加</el-link
                >
                <el-link
                  :icon="Delete"
                  class="mr-3"
                  type="primary"
                  @click="clickDelBtn(scope.row.id)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped></style>
